﻿using System;
using System.Collections.Generic;
using System.Windows;
using Microsoft.Win32;

namespace Songhay.Wpf.UriTree
{
    using Managers;
    using Properties;
    using Songhay.Data.Linq.Opml;

    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class ClientContainer : Application
    {
        /// <summary>
        /// Gets or sets the alternate browser.
        /// </summary>
        /// <value>The alternate browser.</value>
        public static string AlternateBrowser { get; private set; }

        /// <summary>
        /// Gets or sets the application title.
        /// </summary>
        /// <value>The application title.</value>
        public static string ApplicationTitle { get; private set; }

        /// <summary>
        /// Gets or sets a value indicating whether an alternate browser is configured.
        /// </summary>
        /// <value><c>true</c> if an alternate browser is configured; otherwise, <c>false</c>.</value>
        public static bool UseAlternateBrowser { get; set; }

        /// <summary>
        /// Shutdowns the application.
        /// </summary>
        public static void ShutdownApplication()
        {
            if(Current != null) Current.Shutdown();
        }

        /// <summary>
        /// Raises the <see cref="E:System.Windows.Application.Exit"/> event.
        /// </summary>
        /// <param name="e">An <see cref="T:System.Windows.ExitEventArgs"/> that contains the event data.</param>
        protected override void OnExit(ExitEventArgs e)
        {

            if(this._taskbarIconManager != null) this._taskbarIconManager.Dispose();

            base.OnExit(e);
        }

        /// <summary>
        /// Raises the <see cref="E:System.Windows.Application.Startup"/> event.
        /// </summary>
        /// <param name="e">A <see cref="T:System.Windows.StartupEventArgs"/> that contains the event data.</param>
        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);

            this.ShutdownMode = ShutdownMode.OnExplicitShutdown;

            //Last-chance exception handling:
            this.DispatcherUnhandledException += (s, args) =>
            {
                var message = string.Concat(args.Exception.Message, Environment.NewLine, args.Exception.StackTrace);
                MessageBox.Show(message, ApplicationTitle);
            };

            SystemEvents.SessionEnding += (s, args) =>
            {
                ClientContainer.ShutdownApplication();
            };

            Settings.Default.SettingsLoaded += (s, args) =>
            {
                string[] shellArgs = Environment.GetCommandLineArgs();
                UseAlternateBrowser = (Array.IndexOf(shellArgs, "-altbrowser") != -1);

                AlternateBrowser = Environment.ExpandEnvironmentVariables(Settings.Default.AlternateBrowser);
                ApplicationTitle = Settings.Default.AppTitle;

                var opmlMetaData = new OpmlMetaData();
                opmlMetaData.XPathQueries = new List<NamedXPath>()
                {
                    { new NamedXPath { XPathName = XPathName.BodyOutline, XPath = Settings.Default.XpathOpmlBody } },
                    { new NamedXPath { XPathName = XPathName.OutlineText, XPath = Settings.Default.XpathUriName } },
                    { new NamedXPath { XPathName = XPathName.OutlineUrl, XPath = Settings.Default.XpathUriValue } }
                } as IList<INamedXPath>;

                this._taskbarIconManager = new TaskbarIconManager(opmlMetaData);
                this._taskbarIconManager.LoadOpml();

            };

            //Calling a setting makes sure that Settings.Default.SettingsLoaded() will fire:
            ApplicationTitle = Settings.Default.AppTitle;
        }

        TaskbarIconManager _taskbarIconManager;
    }
}
